package com.design.mode.single;

import java.util.concurrent.TimeUnit;

/**
 * 双检锁模式
 *
 * @Date 2023/3/29 15:08:19
 * @Author luopeng
 */
public class SingleObjectDoubleCheckedLocking {

    private static SingleObjectDoubleCheckedLocking object;

    private SingleObjectDoubleCheckedLocking() {
    }

    /**
     * 双检锁模式获取实例
     *
     * @return
     */
    public static SingleObjectDoubleCheckedLocking getSingleObject() {
        if (object == null) {
            synchronized (SingleObjectDoubleCheckedLocking.class) {
                try {
                    TimeUnit.MILLISECONDS.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (object == null) {
                    object = new SingleObjectDoubleCheckedLocking();
                }
            }
        }
        return object;
    }

    public void showMessage() {
        System.out.println("Hello, World!");
    }
}
